package com.fayuan.nocv.controller;

import com.alibaba.fastjson.JSONObject;
import com.fayuan.nocv.entity.ChinaTotal;
import com.fayuan.nocv.entity.CiYun;
import com.fayuan.nocv.entity.NocvData;
import com.fayuan.nocv.entity.NocvNews;
import com.fayuan.nocv.service.ChinaTotalService;
import com.fayuan.nocv.service.CiYunService;
import com.fayuan.nocv.service.IndexService;
import com.fayuan.nocv.service.NocvNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("index")
public class IndexController {
    @Autowired
    private IndexService indexService;
    @Autowired
    private ChinaTotalService chinaTotalService;
    @Autowired
    private NocvNewsService nocvNewsService;
    @Autowired
    private CiYunService ciYunService;

    @RequestMapping("/china")
    public String toChina(Model model){
        Integer id = chinaTotalService.maxID();
        ChinaTotal chinaTotal = chinaTotalService.getById(id);
        List<NocvNews> newsList = nocvNewsService.only3list();
        Date updateTime = chinaTotal.getUpdateTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(updateTime);

        Jedis jedis = new Jedis("127.0.0.1");
            ChinaTotal chinaTotalRedis = new ChinaTotal();
            String confirm = jedis.get("confirm");
            String input = jedis.get("input");
            String dead = jedis.get("dead");
            String heal = jedis.get("heal");
            String updateTime1 = jedis.get("updateTime");
            if (confirm!=null
                    &&input!=null
                    &&dead!=null
                    &&heal!=null
                    &&updateTime1!=null){
                chinaTotalRedis.setConfirm(Integer.parseInt(confirm));
                chinaTotalRedis.setInput(Integer.parseInt(input));
                chinaTotalRedis.setDead(Integer.parseInt(dead));
                chinaTotalRedis.setHeal(Integer.parseInt(heal));

            model.addAttribute("updateTime",updateTime1);
            model.addAttribute("ChinaTotal",chinaTotalRedis);
            model.addAttribute("newsList",newsList);
            return "china";
        }else {
            model.addAttribute("updateTime",format);
            model.addAttribute("ChinaTotal",chinaTotal);
            model.addAttribute("newsList",newsList);

            jedis.set("confirm", String.valueOf(chinaTotal.getConfirm()));
            jedis.set("input", String.valueOf(chinaTotal.getInput()));
            jedis.set("dead", String.valueOf(chinaTotal.getDead()));
            jedis.set("heal", String.valueOf(chinaTotal.getHeal()));
            jedis.set("updateTime",format);
            return "china";
        }
    }

    @RequestMapping("/")
    public String index(Model model, HttpSession session){
        Integer id = chinaTotalService.maxID();
        ChinaTotal chinaTotal = chinaTotalService.getById(id);
        Date updateTime = chinaTotal.getUpdateTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(updateTime);
        model.addAttribute("updateTime",format);
        model.addAttribute("ChinaTotal",chinaTotal);
        return "index";
    }
    @GetMapping("/ciyun")
    @ResponseBody
    public List<CiYun>CiYun(){

      return   ciYunService.list();
    }


    @RequestMapping("/query")
    @ResponseBody
    public List<NocvData>queryAll(){
        List<NocvData> list = indexService.lastChinaMapData();
        Jedis jedis = new Jedis("127.0.0.1");
        List<String> redisNocv = jedis.lrange("nocvData", 0, 33);
        List<NocvData> redisList = new ArrayList<>();
        if (redisNocv.size()>0){

            for (String s : redisNocv) {
                NocvData nocvData = new NocvData();
                JSONObject jsonObject = JSONObject.parseObject(s);
                Object name = jsonObject.get("name");
                Object value = jsonObject.get("value");
                nocvData.setName(String.valueOf(name));
                nocvData.setValue(Integer.parseInt(String.valueOf(value)));
                redisList.add(nocvData);
            }
            return redisList;
        }else {
            for (NocvData nocvData : list) {
                String s = JSONObject.toJSONString(nocvData);
                jedis.lpush("nocvData",s);
            }
            return list;
        }

    }
    @GetMapping("/pie")
    public String toPie(Model model){
        List<NocvNews> newsList = nocvNewsService.only3list();
        model.addAttribute("newsList",newsList);
        return "pie";
    }
    @GetMapping("/bar")
    public String toBar(){
        return "bar";
    }
    @GetMapping("/line")
    public String toLine(){ return "line";}
    @GetMapping("/world")
    public String toWorld(){return "world";}
    @GetMapping("admin/chinaMapManage")
    public String toChinaMapManage(){return "admin/chinaMapManage";}
    @GetMapping("admin/healthClock")
    public String toHealthClock(){return "admin/healthClock";}
    @GetMapping("/login")
    public String tologin(){
        return "login";
    }
    @GetMapping("/menu")
    public String toMenu(){return "menu/menu";}
    @GetMapping("/role")
    public String toRule(){return "role/role";}
    @GetMapping("/user")
    public String toUser(){return  "user/user";}
    @GetMapping("/UserInfo")
    public String toUserInfo(){return "user/userInfo";}
    @GetMapping("/ChangePassword")
    public String toChangePassword(){return "user/changepassword";}
    @GetMapping("/news")
    public String toNews(){return "news/news";}
    @GetMapping("/office")
    public String toOfficeAdmin(){return "office/office";}
    @GetMapping("/department")
    public String toDepartmentAdmin(){return "department/department";}
    @GetMapping("/hesuan")
    public String toHesuan(){return "hesuan/hesuan";}
    @GetMapping("/vaccine")
    public String toVaccine(){return "vaccine/vaccine";}
    @GetMapping("/myHesuan")
    public String toMyHeSuan(){return "hesuan/MyHesuan";}
    @GetMapping("/myVaccine")
    public String toMyVaccine(){return "vaccine/MyVaccine";}
    @GetMapping("/chatgpt")
    public String toChargpt(){return "chatgpt";}

    @RequestMapping("/queryPie")
    @ResponseBody
    public List<NocvData>queryPieAll(){

        return indexService.queryPie();
    }

    @GetMapping("/queryBar")
    @ResponseBody
    public Map<String, List<Object>> queryBarAll(){
        Map<String, List<Object>> stringListMap = indexService.queryBar();
        return stringListMap;
    }


}
